"use strict";

module.exports = (app) => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const Dict = app.model.define(
    "dict",
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      dict_name: { type: STRING(100), allowNull: false },
      dict_code: { type: STRING(255), allowNull: false },
      description: { type: STRING(255), allowNull: true },
      created_at: DATE,
      updated_at: DATE,
    },
    {
      tableName: "dict",
      timestamps: true,
      underscored: true,
      comment: "字典类型表",
      indexes: [{ unique: true, fields: ["dict_code"] }],
    }
  );

  // 关联字典数据
  Dict.associate = function () {
    this.hasMany(app.model.DictItem, {
      foreignKey: "dict_id",
      as: "dict_item",
    });
  };

  return Dict;
};
